import unittest

"""Falta o da rotação de matri
"""
#TODO: rotação de matriz

def allUnique(ss):
    """ Exercicio 1.1"""
    if ss==None:
        raise IndexError()
    for i in xrange(len(ss)-1):
        for j in xrange(i+1,len(ss)):
            if ss[i]==ss[j]:
                return False
    return True

class allUniqueTest(unittest.TestCase):
    
    knownInputs = (('abc',True),
                   ('abcda',False),
                   ('a',True),
                   ('a bc',True),
                   ('aqwertyuiopsdfghjkl',True),
                   ('aa',False),
                   ('qwertyuiopasdfghjklxcvbnmqsdfghj',False)
              )
    
    def testKnownInputs(self):
        for input,result in self.knownInputs:
            self.assertEqual(allUnique(input), result)
            
    
    def testEmpty(self):
        self.assertTrue(allUnique(''))
        
    def testNone(self):
        self.assertRaises(IndexError,allUnique,None)
        
        
def reverse(ss):
    """ 1.2"""
    if ss==None: raise TypeError("None is not valid input")
    li = [c for c in ss]
    half=len(li)/2
    size = len(li)
    for i in xrange(half):
        tmp = li[i]
        li[i]=li[size-i-1]
        li[size-i-1]=tmp
    return ''.join(li)
        
class TestReverse(unittest.TestCase):
    inputs = (('a','a'),
              ('ab','ba'),
              ('abc','cba'),
              ('abcd','dcba'),
              ('aaabaaa','aaabaaa'),
              ('','')
              )
    def testInputs(self):
        for input,result in self.inputs:
            self.assertEqual(reverse(input), result)
            
def removeDuplicate(ss):
    "ex: 1.3"
    pass

def anagram(a,b):
    li1 = [c for c in a]
    li2 = [c for c in b]
    li1.sort()
    li2.sort()
    return ''.join(li1)==''.join(li2)
    

class TestAnagram(unittest.TestCase):
    inputs = (
             ('abc','cba',True),
             ('abcd','cbae',False),
             ('aaaa','aaaa',True),
             ('cccaaaa','acacaca',True),
             ('','',True),
             ('','a',False),
             ('aa','aba',False)
             )
    def testInputs(self):
        for in1,in2,result in self.inputs:
            self.assertEqual(anagram(in1, in2),result)
            

def replace(ss,old,new):
    novo=[]
    for c in ss:
        if c==old:
            novo.append(new)
        else:
            novo.append(c)
    return ''.join(novo)
            
class TestReplace(unittest.TestCase):
    inputs = (
              ('abc 123 asd','abc%20123%20asd'),
              ('',''),
              ('  ','%20%20'),
              )
    
    def testInputs(self):
        for input,result  in self.inputs:
            self.assertEqual(replace(input,' ','%20'), result)
            
def zeroRowsColumns(mat):
    rows = len(mat)
    cols=0
    if rows:
        cols=len(mat[0])
    toZero=[]
    for i in xrange(rows):
        for j in xrange(cols):
            if mat[i][j]==0:
                toZero.append((i,j))
    for row,col in toZero:
        for i in xrange(cols):
            mat[row][i]=0
        for i in xrange(rows):
            mat[i][col]=0
                
class TestZeroRowsCols(unittest.TestCase):
    
    inputs = (
              ([[1,1,0,1],
                [1,1,1,1],
                [1,1,1,1],
                [1,1,1,1]],
               [[0,0,0,0],
                [1,1,0,1],
                [1,1,0,1],
                [1,1,0,1]]
                ),
              ([[1]],[[1]]),
              ([[]],[[]]),
              ([[1, 1, 1, 0]],[[0,0,0,0]])
              )
    
    def testInputs(self):
        for input,result in self.inputs:
            zeroRowsColumns(input)
            rows = len(result)
            cols=0
            if rows>0:
                cols=len(result[0]) 
            for i in xrange(rows):
                for j in xrange(cols):
                    self.assertEqual(result[i][j],input[i][j])
        print 'zero OK'
        
        
def useSubstring(s1,s2):
    if len(s1)!=len(s2):
        return False
    return (s1+s1).count(s2)>0

class TestUseSubstring(unittest.TestCase):
    inputs = (
              ('waterbottle','erbottlewat',True),
              ('asd','dsaa',False),
              ('test','stte',True),
              ('','',True),
              ('abc','cad',False)
              )
    def testInputs(self):
        for in1,in2,res in self.inputs:
            self.assertEqual(useSubstring(in1, in2), res)
                
            